#pragma once

#include <AVUIFrameworkElement.h>
    
namespace AVUI {

class ItemsControl;
class ItemContainerGenerator;
class ItemsPanelTemplate;
class Panel;

class ItemsPresenter : public FrameworkElement
{
public:
    ItemsPresenter() { };

    DECLARE_ELEMENT(ItemsPresenter, FrameworkElement);

    DECLARE_DEPENDENCY_PROPERTY(Template);
    TRefCountedPtr<ItemsPanelTemplate> get_Template() { return (ItemsPanelTemplate*)(Object*)GetValue(get_TemplateProperty()); }
    void set_Template(ItemsPanelTemplate* pValue) { SetValue(get_TemplateProperty(), (Object*)pValue); } 

    static ItemsPresenter* FromPanel(Panel* pPanel);
    ItemsControl* get_Owner() { return m_pOwner; }
    ItemContainerGenerator* get_Generator() { return m_pGenerator; }
protected:

    virtual Size MeasureOverride(const Size& constraintSize);
    virtual Size ArrangeOverride(const Size& constraintSize);

    virtual void OnPreApplyTemplate();
    virtual void OnApplyTemplate();

    virtual FrameworkTemplate* get_TemplateInternal() { return (FrameworkTemplate*) (ItemsPanelTemplate*)get_Template(); }

private:
    static void StaticConstructor();

    void AttachToOwner();
    void UseGenerator(ItemContainerGenerator* pGenerator);

    // Update template cache?
    static void OnTemplateChanged(DependencyObject* pDO, DependencyPropertyChangedEventArgs* e) { }

    TRefCountedPtr<ItemContainerGenerator> m_pGenerator;
    TWeakPtr<ItemsControl> m_pOwner;
};



}; // namespace AVUI


